//
//  WBNavigationController.swift
//  YLWeibo
//
//  Created by 楊亜玲 on 16/11/9.
//  Copyright © 2016年 楊亜玲. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 隐藏默认的navigationBar
        navigationBar.isHidden = true
        
    }

    
    /// 重写push方法(所有的push动作都会调用此方法)
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        // 1. 隐藏底部tabbar (如果不是栈底控制器才需要隐藏，根控制器不需要处理)
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            
            // 2. 设置返回按钮
            if let vc = viewController as? WBBaseViewController{
                var title = "返回"
                
                // 控制器级数（只有1个子控制器的时候，显示栈底控制器的标题）
                if childViewControllers.count == 1  {
                    title = childViewControllers.first?.title ?? "返回"
                }
                
                vc.naviItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action:#selector(popToParent),isBackButton: true)
                
            }
        }
        
        super.pushViewController(viewController, animated: true)
       
    }
    
    /// 返回上一级控制器
    func popToParent(){
        popViewController(animated: true)
    }

}
